<?php

namespace app\index\controller;

use app\admin\model\LinksMessage;
use app\admin\model\LinksMessageViewLog;
use app\admin\model\MemberLevel;
use app\admin\model\PostClickShare;
use app\admin\model\ResourceClickShare;
use app\admin\model\ResourceCollect;
use app\admin\model\UsersMember;
use app\index\model\Order;
use app\index\model\PostComment;
use app\index\model\PostCommentLike;
use app\index\model\PostLike;
use app\index\model\ResourceCheck;
use app\index\model\UsersCateModel;
use app\index\model\UsersLoginRecordModel;
use app\index\model\UsersModel;
use think\facade\Db;
use think\facade\Log;
use wxpay\Wxpay;
use app\admin\model\Resource as iResource;
use app\index\model\Post as PostModel;

/**
 * 用户
 * Class Users
 * @package app\index\controller
 */
class Users extends Base
{

    /**
     * 初始化sdk
     */
    public function initJsSdk()
    {
        if (!$this->request->isGet()) {
            $this->responseJson(10500, '发生异常');
        }

        $aims_url = urldecode($this->request->param('url'));

        $timestamp = time();
        $nonceStr = createNonceStr();
        $Ticken = new Ticken();
        $jsapiTicket = $Ticken->GetTicken();
        // $aims_url = "http://wx2.xnm3000.cn/index/user/test";
        $log['jsapiTicket_str'] = $jsapiTicket;
        $log['jsapiTicket_date'] = date('Y-m-d H:i:s');
        Log::write($log, 'error');

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$aims_url";
        $signature = sha1($string);

        $data = array(
            "appId" => 'wxe4b77d2f0f456b9b',
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "url" => $aims_url,
            "signature" => $signature,
            "rawString" => $string
        );

        $this->responseJson(10200, 'success', $data);
    }

    /**
     * 公众号授权
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function publicAuth()
    {
        // https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
        $wechat = new Wechat();
        Log::write('获取的GET参数:' . json_encode($_GET), 'error');

        // 仅作为测试
//        if ($this->request->header('tt')) {
//            $info = UsersModel::getUserInfo(['token1'=>'2a54f2ddf4e69c9976147fc318464ffcf63890bf']);
//            $this->responseJson(10200, 'success', $info);
//        }

        // 没有code，去微信接口获取code码
        if (!isset($_GET['code'])) {
            // 微信服务器回调url（需在开发者后台设置），这里是本页url
            $callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '/users/publicAuth';
            Log::write('code不存在，我是回调url:' . $callback, 'error');
            $wechat->getCode($callback);
        } else {
            Log::write('我是code回调后的操作，获取的GET参数:' . json_encode($_GET), 'error');

            $accessToken = $wechat->accessToken($_GET['code']);
            Log::write('access_token数据:' . json_encode($accessToken), 'error');

            $wechatInfo = $wechat->getWechatInfo($accessToken['access_token'], $accessToken['openid']);
            Log::write('公众号授权获取获取到的wechatInfo:' . json_encode($wechatInfo), 'error');

            if (!$wechatInfo['openid']) {
                Log::write('openid获取失败', 'error');
                $this->responseJson(10500, '公众号授权登录失败');
            }

            // 暂时更改，不在此做数据合并
//            $update = [];
//            if (isset($wechatInfo['unionid'])) {
//                $users = UsersModel::where('unionid', $wechatInfo['unionid'])->find();
//                $update['unionid'] = $wechatInfo['unionid'];
//            }
//
//            if (!isset($users)) {
//                $users = UsersModel::where('pub_openid', $wechatInfo['openid'])->find();
//            }

            $users = UsersModel::where('pub_openid', $wechatInfo['openid'])->find();

            // 添加
//            if (!isset($users)) {
            if (empty($users)) {
                // 获取最新的用户id
                $lastId = UsersModel::order('id', 'desc')->value('id');
                $lastId++;
                $datas = [
                    'pub_openid' => $wechatInfo['openid'],
                    'unionid' => isset($wechatInfo['unionid']) ? $wechatInfo['unionid'] : '',
                    'nickname' => isset($wechatInfo['nickname']) ? emojiEncode($wechatInfo['nickname']) : '游客' . $lastId,
                    'sex' => isset($wechatInfo['sex']) ? $wechatInfo['sex'] : -1,
                    'country' => isset($wechatInfo['country']) ? $wechatInfo['country'] : '',
                    'province' => isset($wechatInfo['province']) ? $wechatInfo['province'] : '',
                    'city' => isset($wechatInfo['city']) ? $wechatInfo['city'] : '',
                    'headimg' => isset($wechatInfo['headimgurl']) ? $wechatInfo['headimgurl'] : '',
                    // 授权状态  1已授权  -1未授权
                    'status' => 1,
                    // 标识账号所在端
                    'associate' => 1,
                ];
                $res = UsersModel::create($datas);
                if (!$res) {
                    $this->responseJson(10500, 'User Create Error');
                }
                $users = $datas;
                $users['id'] = $res->id;
            }
            // token生成
            $token = md5($wechatInfo['openid']);
            $update['token1'] = $token;
            // 更新token
            $res = UsersModel::update($update, ['pub_openid' => $wechatInfo['openid']]);
            if (!$res) {
                $this->responseJson(10500, 'User Update Error');
            }
            unset($users['token'], $users['token1']);
            $users['token'] = $token;
            $this->responseJson(10200, 'success', $users);
        }

    }

    /**
     * 会员级别
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function memberLevel()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '获取会员级别列表', 'error');

        $data = MemberLevel::where('status', 1)
            ->field('id,level,price,validity,unit,privilege,images')
            ->order('sort', 'desc')
            ->select();
        foreach ($data as $k => $v) {
            if ($v['images']) {
                $data[$k]['images'] = config('url.url') . $v['images'];
            }
            if ($v['privilege']) {
                $data[$k]['privilege'] = explode('|', $v['privilege']);
            }
        }
        $this->responseJson(10200, 'success', $data);
    }

    /**
     * 小程序成为会员
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function beComeMember()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '小程序成为会员', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        if (!isset($param['uid']) && !isset($param['mid'])) {
            $this->responseJson(10500, '参数错误');
        }

        // 查询会员级别
        $memberLevel = MemberLevel::where('id', $param['mid'])->find();
        if (empty($memberLevel)) {
            $this->responseJson(10500, '请选择会员级别信息');
        }
        // 会员信息
        $usersMember = UsersMember::where(['uid' => $param['uid'], 'is_show' => 1])->find();
        // 用户信息
        $users = UsersModel::where('id', $param['uid'])->find();
        // 是否是会员
        if (!empty($usersMember)) {
            // 续费 获取原剩余时间累加
            $users['is_member'] = 1;
            $endTime = UsersMember::setMemberExpireTime($memberLevel, $usersMember['end_time']);
        } else {
            // 新会员
            $users['is_member'] = 0;
            $endTime = UsersMember::setMemberExpireTime($memberLevel, time());
        }

        $order = [];
        $order['uid'] = $users->id;
        $order['unionid'] = $users->unionid;
        $order['order_no'] = generateOrderNo();
        $order['mid'] = $memberLevel->id;
        $order['price'] = $memberLevel->price;
        // 支付来源 1小程序  2公众号
        $order['pay_source'] = 1;
        // 订单类型：1 => 会员购买
        $order['type_id'] = 1;
        $order['desc'] = $users->is_member == 1 ? "老会员续费" : '新会员购买';
        // 订单写入
        Order::create($order);

        $wxPay = [
            'order_sn' => $order['order_no'],  //支付订单号
            'total' => $order['price'],   //支付总额
//            'total' => 0.1,   //支付总额
            'body' => $order['desc'],   //支付说明
            'userip' => '0.0.0.0',   //用户IP
        ];

        $pay = new Wxpay(9);
        $result = $pay->index($wxPay, $users->openid);

        $this->responseJson(10200, 'success', ['data' => $memberLevel, 'end_time' => $endTime, 'pay_param' => $order, 'result' => $result]);

    }

    /**
     * 公众号成为会员
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function publicBeComeMember()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . $userInfo['nickname'] . '公众号成为会员', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        if ($param['uid'] != $userInfo['id']) {
            $this->responseJson(10500, '不允许的操作');
        }

        if (!isset($param['uid']) && !isset($param['mid'])) {
            $this->responseJson(10500, '参数错误');
        }

        // 查询会员级别
        $memberLevel = MemberLevel::where('id', $param['mid'])->find();
        if (empty($memberLevel)) {
            $this->responseJson(10500, '请选择会员级别信息');
        }
        // 会员信息
        $usersMember = UsersMember::where(['uid' => $param['uid'], 'is_show' => 1])->find();
        // 用户信息
        $users = UsersModel::where('id', $param['uid'])->find();
        // 是否是会员
        if (!empty($usersMember)) {
            // 续费 获取原剩余时间累加
            $users['is_member'] = 1;
            $endTime = UsersMember::setMemberExpireTime($memberLevel, $usersMember['end_time']);
        } else {
            // 新会员
            $users['is_member'] = 0;
            $endTime = UsersMember::setMemberExpireTime($memberLevel, time());
        }

        $order = [];
        $order['uid'] = $users->id;
        $order['unionid'] = $users->unionid;
        $order['order_no'] = generateOrderNo();
        $order['mid'] = $memberLevel->id;
        $order['price'] = $memberLevel->price;
        // 支付来源 1小程序  2公众号
        $order['pay_source'] = 2;
        // 订单类型：1 => 会员购买
        $order['type_id'] = 1;
        $order['desc'] = $users->is_member == 1 ? "老会员续费" : '新会员购买';
        // 订单写入
        Order::create($order);

        $wxPay = [
            'order_sn' => $order['order_no'],  //支付订单号
            'total' => $order['price'],   //支付总额
//            'total' => 0.1,   //支付总额
            'body' => $order['desc'],   //支付说明
            'userip' => '0.0.0.0',   //用户IP
        ];

        $pay = new Wxpay(10);
        $result = $pay->index($wxPay, $users->pub_openid);

        $this->responseJson(10200, 'success', ['data' => $memberLevel, 'end_time' => $endTime, 'pay_param' => $order, 'result' => $result]);

    }

    /**
     * 小程序微信支付回调
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function callbackNotify()
    {
        Log::write('小程序微信支付回调', 'error');
        // 获取xml
        $xml = file_get_contents("php://input");
        $Wxpay = new Wxpay();
        // 转成php数组
        $data = $Wxpay->toArray($xml);

        // 保存原sign
        $data_sign = $data['sign'];
        // sign不参与签名
        unset($data['sign']);
        $sign = $Wxpay->makeSign($data);
        // 判断签名是否正确  判断支付状态
        if ($sign === $data_sign && $data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
            $result = $data;
        } else {
            $result = false;
        }

        // 返回状态给微信服务器
        if ($result) {

            //处理支付回调订单问题
            Order::orderNotify((array)$data);
            Log::write($data, 'error');

            $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        } else {
            $str = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        }

        return $str;
    }

    /**
     * 公众号微信支付回调
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function publicCallbackNotify()
    {
        Log::write('公众号微信支付回调', 'error');
        // 获取xml
        $xml = file_get_contents("php://input");
        $Wxpay = new Wxpay();
        // 转成php数组
        $data = $Wxpay->toArray($xml);

        // 保存原sign
        $data_sign = $data['sign'];
        // sign不参与签名
        unset($data['sign']);
        $sign = $Wxpay->makeSign($data);
        // 判断签名是否正确  判断支付状态
        if ($sign === $data_sign && $data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
            $result = $data;
        } else {
            $result = false;
        }

        // 返回状态给微信服务器
        if ($result) {

            $data['pay_type'] = 1;
            //处理支付回调订单问题
            Order::orderNotify((array)$data);
            Log::write($data, 'error');

            $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        } else {
            $str = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        }

        return $str;
    }

    /**
     * 测试支付回调
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function testNotify()
    {
        $data = [
            'out_trade_no' => 123456,
            'openid' => 'oRQGa5B4oqINRxmy5wrHPm6n8X1A',
            'time_end' => date('Y-m-d H:i:s', time())
        ];
        Order::orderNotify((array)$data);
    }

    /**
     * 关联其它端
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function associateOtherEnd()
    {
        $userInfo = $this->checkLogin();
        Log::write('用户:' . json_encode($userInfo) . '关联其他端数据', 'error');

        if (!$this->request->isPost()) {
            $this->responseJson(10500, '发生异常');
        }

        $param = $this->request->param();
        // 1关联公众号  2关联小程序
        if (!isset($param['associate']) || !in_array($param['associate'], [1, 2])) {
            $this->responseJson(10500, '参数错误');
        }

        $isExists = UsersModel::where([
            'unionid' => $userInfo['unionid'],
            'associate' => $param['associate']])
            ->whereRaw('id != ' . $userInfo['id'])
            ->field('id,nickname,openid,unionid,pub_openid,associate,token1')->find();
        Log::write('获取用户信息：' . json_encode($userInfo) . '，存在其他端用户关联数据：' . json_encode($isExists), 'error');
//        var_dump($isExists);
//        exit;
        if (empty($isExists)) {
            $this->responseJson(10400, 'empty');
        }

        $aType = $param['associate'] == 1 ? '公众号' : '小程序';
        Log::write('当前登录用户ID：' . $userInfo['id'] . '，关联的用户ID：' . $isExists['id'] . '，关联类型：关联到' . $aType, 'error');

//        echo '当前登录用户ID：' . $userInfo['id'] . PHP_EOL . PHP_EOL;
//        echo '关联的用户ID：' . $isExists['id'] . PHP_EOL . PHP_EOL;

        // 修改条件
        $where = ['uid' => $isExists['id']];
        // 修改数据
        $yWhere = ['uid' => $userInfo['id']];

        // 以当前所在端的数据为新
        Db::startTrans();
        try {

            // 用户表 yl_admin_users
            // 合并关联前的用户数据
            if ($param['associate'] == 1) {
                $update = [
                    'pub_openid' => $isExists['pub_openid'],
                    'token1' => $isExists['token1']
                ];
                // -1关联前公众号
                $associate = -1;
            } else {
                $update = [
                    'openid' => $isExists['openid'],
                    'token' => $isExists['token']
                ];
                // -2关联前小程序
                $associate = -2;
            }
            $update['associate'] = 0;
            // 关联前的用户
            UsersModel::update(['associate' => $associate], ['id' => $isExists['id']]);
            UsersModel::update($update, ['id' => $userInfo['id']]);

            // 会员用户表  yl_admin_users_member
            $wheres = $where;
            $wheres['is_show'] = 1;
            $yWheres = $yWhere;
            $yWheres['is_show'] = 1;
            $usersMember = UsersMember::where($wheres)->find();
//            var_dump($usersMember);
            // 数据存在，合并会员到期时间
            if (!empty($usersMember)) {
                Log::write('《会员用户表》数据存在，合并会员到期时间', 'error');
                $yUsersMember = UsersMember::where($yWheres)->find();
                // 存在原会员数据
                if (!empty($yUsersMember)) {
                    // 计算剩余会员时间
                    $new_time = $usersMember['end_time'] - time();
                    $end_time = $yUsersMember['end_time'] + $new_time;
                    // 更新到期时间
                    UsersMember::update(['end_time' => $end_time], $yWheres);
                    // 删除关联前的会员数据
                    UsersMember::where($wheres)->delete();
                    Log::write('《会员用户表》存在原会员数据', 'error');
                } else {
                    // 不存在，直接修改成需关联的会员
                    UsersMember::update($yWheres, $wheres);
                    Log::write('《会员用户表》不存在，直接修改成需关联的会员', 'error');
                }
            }
//
            // 会员订单表  yl_admin_order
            $order = Order::where($where)->find();
            // 数据存在，修改购买人id
            if (!empty($order)) {
                $yWhere['desc'] = $order['desc'] . '(关联后的订单合并)';
                Order::update($yWhere, $where);
                Log::write('《会员订单表》数据存在，修改购买人id', 'error');
                unset($yWhere['desc']);
            }

            // 用户登录记录表  yl_admin_users_login_record
            $usersLoginRecord = UsersLoginRecordModel::where($where)->find();
//            var_dump($usersLoginRecord);
            // 数据存在，修改登录人id
            if (!empty($usersLoginRecord)) {
                UsersLoginRecordModel::update($yWhere, $where);
                Log::write('《用户登录记录表》数据存在，修改登录人id', 'error');
            }

            // 用户分类记录表 yl_admin_users_cate
            $usersCate = UsersCateModel::where($where)->find();
            $yUsersCate = UsersCateModel::where($yWhere)->find();
//            var_dump($usersCate1,$usersCate);
//            exit;
            // 数据存在，合并用户所选分类 身份更改
            if (!empty($usersCate)) {
                Log::write('《用户分类记录表》数据存在，合并用户所选分类', 'error');
//                UsersCateModel::update($yWhere, $where);
                // 是否已存在了用户原所选分类
                if (!empty($yUsersCate)) {
                    $cids = explode(',', $usersCate['cids']);
                    $ycids = explode(',', $yUsersCate['cids']);
                    // 对比两个数组。不在原分类，则合并
                    $diffCids = array_diff($cids, $ycids);
//                    var_dump($cids,$ycids,$diffCids);
                    if ($diffCids) {
                        $newCids = rtrim(join(',', array_merge($ycids, $diffCids)), ',');
//                        var_dump($newCids);
                        UsersCateModel::update(['cids' => $newCids], $yWhere);
                        Log::write('《用户分类记录表》合并用户原所选分类', 'error');
                    }
                    // 删除关联前的分类数据
                    UsersCateModel::where($where)->delete();
                    Log::write('《用户分类记录表》存在了用户原所选分类', 'error');
                } else {
                    UsersCateModel::update($yWhere, $where);
                    Log::write('《用户分类记录表》不存在了用户原所选分类，直接修改成需关联的分类', 'error');
                }
            }

            // 社群收藏统计表 yl_admin_resource_collect
            $resourceCollect = ResourceCollect::where($where)->select();
//            var_dump($resourceCollect);
            // 数据存在，修改收藏人id
            if (!empty($resourceCollect)) {
                ResourceCollect::update($yWhere, $where);
                Log::write('《社群收藏统计表》数据存在，修改收藏人id', 'error');
            }

            // 社群点击/分享统计表 yl_admin_resource_click_share
            $resourceClickShare = ResourceClickShare::where($where)->select();
//            var_dump($resourceClickShare);
            // 数据存在，修改点击/分享人id
            if (!empty($resourceClickShare)) {
                ResourceClickShare::update($yWhere, $where);
                Log::write('《社群点击/分享统计表》数据存在，修改点击/分享人id', 'error');
            }

            // 社群审核表 yl_admin_resource_check
            $resourceCheck = ResourceCheck::where($where)->select();
//            var_dump($resourceCheck);
            // 数据存在，修改社群审核发布人id
            if (!empty($resourceCheck)) {
                ResourceCheck::update($yWhere, $where);
                Log::write('《社群审核表》数据存在，修改社群审核发布人id', 'error');
            }

            // 社群表 yl_admin_resource
            $iResource = iResource::where($where)->select();
//            var_dump($iResource);
            // 数据存在，修改社群所属人id
            if (!empty($iResource)) {
                iResource::update($yWhere, $where);
                Log::write('《社群表》数据存在，修改社群所属人id', 'error');
            }

            // 帖子点赞统计表 yl_admin_post_like
            $postLike = PostLike::where($where)->select();
//            var_dump($postLike);
            // 数据存在，修改帖子点赞人id
            if (!empty($postLike)) {
                PostLike::update($yWhere, $where);
                Log::write('《帖子点赞统计表》数据存在，修改帖子点赞人id', 'error');
            }

            // 帖子评论点赞表 yl_admin_post_comment_like
            $postCommentLike = PostCommentLike::where($where)->select();
//            var_dump($postCommentLike);
            // 数据存在，修改帖子评论点赞人id
            if (!empty($postCommentLike)) {
                PostCommentLike::update($yWhere, $where);
                Log::write('《帖子评论点赞表》数据存在，修改帖子评论点赞人id', 'error');
            }

            // 帖子评论表 yl_admin_post_comment
            $postComment = PostComment::where($where)->select();
//            var_dump($postComment);
            // 数据存在，修改帖子评论表
            if (!empty($postComment)) {
                PostComment::update($yWhere, $where);
                Log::write('《帖子评论表》数据存在，修改修改帖子评论表uid', 'error');
            }

            // 帖子点击/分享统计表 yl_admin_post_click_share
            $postClickShare = PostClickShare::where($where)->select();
//            var_dump($postClickShare);
            // 数据存在，修改点击/分享人id
            if (!empty($postClickShare)) {
                PostClickShare::update($yWhere, $where);
                Log::write('《帖子点击/分享统计表》数据存在，修改点击/分享人id', 'error');
            }

            // 帖子表 yl_admin_post
            // 数据存在，
            $post = PostModel::where($where)->select();
//            var_dump($post);
            // 数据存在，修改帖子所属人id
            if (!empty($post)) {
                PostModel::update($yWhere, $where);
                Log::write('《帖子表》数据存在，修改帖子所属人id', 'error');
            }

            // 云links通知表 yl_admin_links_message
            $linksMessage = LinksMessage::where('FIND_IN_SET(' . $isExists['id'] . ',uids)')->select();
            // 数据存在，修改云links通知人id
            if (!empty($linksMessage)) {
                foreach ($linksMessage as $k => $v) {
                    $uidsArr = explode(',', $v['uids']);
                    Log::write('用户数据关联前' . json_encode($uidsArr), 'error');
                    $keys = array_search($isExists['id'], $uidsArr);
                    if ($keys != false) {
                        $keys1 = array_search($userInfo['id'], $uidsArr);
                        if ($keys1 == false) {
                            Log::write('追加不存在关联后的用户数据', 'error');
                            $uidsArr[$keys] = (string)$userInfo['id'];
                        }
                    }
                    Log::write('用户数据关联后' . json_encode($uidsArr), 'error');
                    $uids = rtrim(join(',', $uidsArr), ',');
                    LinksMessage::update(['uids' => $uids], ['id' => $v['id']]);
                    Log::write('《云links通知表》数据存在，修改云links通知人id', 'error');
                }
            }

            // 云links通知查看记录表 yl_admin_links_message_view_log
            $linksMessageViewLog = LinksMessageViewLog::where('id', $isExists['id'])->select();
            // 数据存在，修改云links通知查看人id
            if (!empty($linksMessageViewLog)) {
                LinksMessageViewLog::update($yWhere, $where);
                Log::write('《云links通知查看记录表》数据存在，修改云links通知查看人id', 'error');
            }

            Db::commit();

        } catch (\Exception $e) {
            Db::rollback();
            Log::write($e->getMessage(), 'error');

            $this->responseJson(10500, $e->getMessage());
        }

        $this->responseJson(10200, 'success');

    }

}